package top.mschen.blog.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.mschen.blog.common.vo.BaseResult;

import java.util.Map;

/**
 * 统一异常处理
 */
@ControllerAdvice
public class ExceptionControllerAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionControllerAdvice.class);

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResult<Map> errorHandler(Exception e) {
        BaseResult<Map> rlt = new BaseResult<Map>();
        if (e instanceof TopChenException) {
            TopChenException cException = (TopChenException) e;
            rlt.setCode(cException.getErrorCode());
            rlt.setMessage(e.getMessage());
            return rlt;
        } else {
            LOGGER.error("[系统异常]", e);
            rlt.setCode("9999");
            rlt.setMessage(e.getMessage());
            return rlt;
        }
    }

//    @ExceptionHandler(value =Exception.class)
//    public ModelAndView errorViewHandler(Exception e){
//        if(!(e instanceof TopChenException)) LOGGER.error("[系统异常]", e);
//        ModelAndView view = new ModelAndView("error/500.html");
//        view.addObject("exception",e);
//        return view;
//    }
}
